<template>
  <div>
    <!-- 面包屑导航 -->
    <bread></bread>
    <!-- 卡片区域 -->
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="ordersInfo.query"
            class="input-with-select"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="ordersList" border style="width: 100%">
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="order_number" label="订单编号">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格"> </el-table-column>
        <el-table-column label="是否付款">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.pay_status === '1'"
              >已付款</el-tag
            >
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <el-table-column label="下单时间">
          <template v-slot="data">
            <span>
              {{ data.row.create_time | formatDate }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template>
            <!-- 修改地址按钮 -->
            <el-tooltip
              class="item"
              effect="dark"
              content="修改订单地址"
              placement="top"
            >
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="editAddress"
              ></el-button>
            </el-tooltip>
            <!-- 物流信息 -->
            <el-tooltip
              class="item"
              effect="dark"
              content="物流信息"
              placement="top"
            >
              <el-button
                type="success"
                icon="el-icon-location-outline"
                size="mini"
                @click="logistics"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="ordersInfo.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="ordersInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 修改地址的对话框 -->
    <el-dialog title="修改地址" :visible.sync="editShow" @close="closedDialog">
      <el-form :model="editData" :rules="rules" ref="editForm">
        <el-form-item label="省市区/县" :label-width="'100px'" prop="address1">
          <el-cascader
            v-model="editData.address1"
            :options="cityData"
            style="width: 100%"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" :label-width="'100px'" prop="address2">
          <el-input autocomplete="off" v-model="editData.address2"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editShow = false">取 消</el-button>
        <el-button type="primary" @click="editShow = false">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 物流弹窗 -->
    <el-dialog title="物流进度" :visible.sync="dialogVisible" width="5 0%">
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in logisticsList"
          :key="index"
          :timestamp="activity.time"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import bread from "../bread.vue";
import cityData from "./data.js";
// import usersApi from "..//../api/orders";
// import report from '../../api/report';
import orders from '..//../api/orders';

export default {
  components: { bread },
  data() {
    return {
      ordersInfo: {
        query: "", //查询参数
        pagenum: 1, //当前页码
        pagesize: 10, //每页显示条数
      },
      ordersList: [],
      total: 0, //数据总条数
      dialogVisible: false, //物流数据弹窗
      reverse: true,
      logisticsList: [], //物流数据信息
      editShow: false,
      activities: [
        {
          content: "活动按期开始",
          timestamp: "2018-04-15",
        },
        {
          content: "通过审核",
          timestamp: "2018-04-13",
        },
        {
          content: "创建成功",
          timestamp: "2018-04-11",
        },
      ],
      editData: {
        //修改的相关数据
        address1: [],
        address2: "",
      },
      rules: {
        address1: [
          { required: true, message: "请选择省市区县", trigger: "blur" },
        ],
        address2: [
          { required: true, message: "请填写详细地址", trigger: "blur" },
        ],
      },
      cityData: cityData,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //获取列表数据
    async getList() {
      let res = await orders.getList({ params: this.ordersInfo });
      let { data, meta } = res.data;
      this.total = data.total;
      if (meta.status !== 200) {
        this.$message.error(meta.msg);
      } else {
        this.ordersList = data.goods;
      }
    },
    handleSizeChange(val) {
      this.ordersInfo.pagesize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.ordersInfo.pagenum = val;
      this.getList();
    },
    async logistics() {
      // let res = await this.$http.get("/kuaidi/1106975712662");
      let res = await orders.logistics();

      let { data } = res.data;
      this.logisticsList = data;
      this.dialogVisible = true;
    },
    // 关闭修改地址的对话框
    closedDialog() {
      this.$refs.editForm.resetFields();
    },
    // 点击修改地址的操作按钮
    editAddress() {
      this.editShow = true;
    },
    // 展示物流进度的对话框
    async showProgress() {
      this.progressShow = true; //显示物流信息对话框
      // 请求物流信息数据
      // const { data: res } = await this.$http.get("kuaidi/${id}");
      const { data: res } = await orders.showProgress();
      console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error("获取物流进度失败");
      }
      this.progressData = res.data;
    },
  },
  filters: {
    //过滤器
    //时间戳转日期
    formatDate: function(value) {
      let date = new Date(value * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      let y = date.getFullYear();
      let MM = date.getMonth() + 1;
      MM = MM < 10 ? "0" + MM : MM;
      let d = date.getDate();
      d = d < 10 ? "0" + d : d;
      let h = date.getHours();
      h = h < 10 ? "0" + h : h;
      let m = date.getMinutes();
      m = m < 10 ? "0" + m : m;
      let s = date.getSeconds();
      s = s < 10 ? "0" + s : s;
      return y + "-" + MM + "-" + d + " " + h + ":" + m + ":" + s; //多种时间格式的拼接
      // return y + "-" + MM + "-" + d;
    },
  },
  // components: {
  //   bread,
  // },
};
</script>
<style lang="scss" scoped>
.el-row {
  margin-bottom: 10px;
}
.el-card {
  border-radius: 5px;
  margin-top: 20px;
}
</style>
